package com.hhs.storage.service.impl;

import com.hhs.storage.bean.Article;
import com.hhs.storage.bean.ResponseInfo;
import com.hhs.storage.dao.ArticleDao;
import com.hhs.storage.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhs.storage.util.FileUpload;
import com.hhs.storage.util.SysUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hhs
 * @since 2023-11-30
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleDao, Article> implements ArticleService {
    @Resource
    HttpServletRequest request;
    @Value("${upload.folderName}")
    private String folderName;

    @Override
    public Map<String, Object> addOrUpdate(String id, String content, String module, String deleImgStr) {
        Article article = new Article();
        article.setModule(module);
        article.setContent(content);
        if(StringUtils.isEmpty(id)) {
            String createrId = SysUtil.getLoginUserId(request);
            article.setId(SysUtil.UUID());
            article.setCerator(createrId);
            article.setCreateTime(LocalDateTime.now());
            this.save(article);
        }else{
            article.setId(id);
            this.updateById(article);
        }
        //提交时，要删除的图片
        String[] deleArr = deleImgStr.split(",");
        if(deleArr.length>0){
            for (String name : deleArr) {
                FileUpload.deleteFile(folderName,name);
            }
        }
        return ResponseInfo.success(null);
    }
}
